﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CocisDAL;
using CocisModel;
using CocisModel.Common;
using System.Data;
using CocisWeb.src;

namespace CocisWeb.ManageAsset
{
    public partial class AddNetwork : System.Web.UI.Page
    {
        public AssetDAL dal = new AssetDAL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindDropDownList();
            }
        }

        public void BindDropDownList()
        {
            this.ddlTypeName.DataSource = dal.GetType();
            this.ddlTypeName.DataTextField = "TypeName";
            this.ddlTypeName.DataValueField = "TypeID";
            this.ddlTypeName.DataBind();

            this.ddlSiteCode.DataSource = dal.GetSiteCode();
            this.ddlSiteCode.DataTextField = "SiteCode";
            this.ddlSiteCode.DataValueField = "SiteCodeID";
            this.ddlSiteCode.DataBind();

            this.ddlRackName.DataSource = dal.GetRack();
            this.ddlRackName.DataTextField = "RackName";
            this.ddlRackName.DataValueField = "RackID";
            this.ddlRackName.DataBind();

            this.ddlHostingType.DataSource = dal.GetHostingType();
            this.ddlHostingType.DataTextField = "HostingType";
            this.ddlHostingType.DataValueField = "HostingTypeID";
            this.ddlHostingType.DataBind();

            this.ddlStatus.DataSource = dal.GetStatus();
            this.ddlStatus.DataTextField = "Status";
            this.ddlStatus.DataValueField = "StatusID";
            this.ddlStatus.DataBind();

            this.ddlPduName.DataSource = dal.GetPDU();
            this.ddlPduName.DataTextField = "PduName";
            this.ddlPduName.DataValueField = "PduID";
            this.ddlPduName.DataBind();

            this.ddlPduPort.DataSource = dal.GetPDU();
            this.ddlPduPort.DataTextField = "PduPort";
            this.ddlPduPort.DataValueField = "PduID";
            this.ddlPduPort.DataBind();

            this.ddlVendorName.DataSource = dal.GetVendor();
            this.ddlVendorName.DataTextField = "VendorName";
            this.ddlVendorName.DataValueField = "VendorID";
            this.ddlVendorName.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Asset.tbNetworkDataTable dt = new Asset.tbNetworkDataTable();
            Asset.tbNetworkRow rows = dt.NewtbNetworkRow();
            Guid gd = new Guid();
            rows.NetworkID = gd;
            rows.TypeID = int.Parse(this.ddlTypeName.SelectedValue.ToString());
            rows.AssetNo = this.txtAssetNo.Text;
            rows.UniqueNo = this.txtUniqueNo.Text;
            rows.Brand = this.txtBrand.Text;
            rows.Model = this.txtModel.Text;
            rows.SiteCodeID = int.Parse(this.ddlSiteCode.SelectedValue.ToString());
            rows.Floors = this.txtFloors.Text;
            rows.RackID = int.Parse(this.ddlRackName.SelectedValue.ToString());
            rows.PositionInRack = this.txtPositionInRack.Text;
            rows.ManageIP = this.txtManageIP.Text;
            rows.Capacity100Mbps = this.txtCapacity100Mbps.Text;
            rows.Capacity1Gbps = this.txtCapacity1Gbps.Text;
            rows.Capacity10Gbps = this.txtCapacity10Gbps.Text;
            rows.UplinkInfo = this.txtUplinkInfo.Text;
            rows.HostingTypeID = int.Parse(this.ddlHostingType.SelectedValue.ToString());
            rows.StatusID = int.Parse(this.ddlStatus.SelectedValue.ToString());
            rows.PduID = int.Parse(this.ddlPduName.SelectedValue.ToString());
            rows.PduID = int.Parse(this.ddlPduPort.SelectedValue.ToString());
            rows.SN = this.txtSN.Text;
            rows.WarrantyStartDate = DateTime.Parse(this.txtWarrantyStartDate.Text);
            rows.WarrantyEndDate = DateTime.Parse(this.txtWarrantyEndDate.Text);
            rows.VendorID = int.Parse(this.ddlVendorName.SelectedValue.ToString());

            dt.Rows.Add(rows);
            dt.AcceptChanges();

            int temp = dal.AddNetwork(dt);
            if (temp > 0)
            {
                string ip = Request.UserHostAddress.ToString();
                ClientScript.RegisterStartupScript(this.GetType(), "a", "<script language=javascript>alert('添加成功！')</script>");
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "b", "<script language=javascript>alert('添加失败！')</script>");
            }

        }
    }
}